<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>物料管理</title>
    <style>
        .newtitle {
            PADDING-LEFT: 15px;
            BACKGROUND: url('');
            PADDING-BOTTOM: 4px;
            BORDER-LEFT: #c0dcf2 1px solid;
        }
    </style>
    <th:block th:include="common.html :: source"></th:block>
    <script type="text/javascript">
        //初始化分页组件
        $(function () {
            var pager = $(".easyui-datagrid").datagrid('getPager');
            $(pager).pagination({
                total:parseInt($("#total").val()),
                pageSize:parseInt($("#size").val()),
                pageNumber:parseInt($("#current").val()),
                afterPageText: '页，共{pages}页',
                pageList: [5, 10, 20, 30, 50, 100],
                //刷新方法
                onBeforeRefresh: function (current, size) {
                    location.href = "/material/list?current=" + current + "&size=" + size;
                },
                onSelectPage: function (current, size) {
                    location.href = "/material/list?current=" + current + "&size=" + size;
                }
            });
        });

        function del(materialId){
            $.messager.confirm('提示','确定删除？',function(res){
                if(res){
                    $.post(
                        "/material/delete?id="+materialId,
                        function (data) {
                            if(data == "success"){
                                $.messager.alert("操作提示", "删除成功！","info",function () {
                                    window.location.href = "/material/list";
                                });
                            }else{
                                $.messager.alert("操作提示", "删除失败！","error");
                            }
                        }
                    );
                }
            })
        }

        //保存修改
        function save() {
            if ($("#materialCode").val()==""){
                $.messager.alert("操作提示", "请输入物料编码","error",function () {
                    $("#materialCode").focus();
                });
                return false;
            }
            if ($("#materialName").val()==""){
                $.messager.alert("操作提示", "请输入物料名称","error",function () {
                    $("#materialName").focus();
                });
                return false;
            }

            $.post(
                "/material/save",
                {
                    "materialId":$("#materialId").val(),
                    "materialName":$("#materialName").val(),
                    "prodTypeId":$("#prodTypeId").val(),
                    "materialCode":$("#materialCode").val(),
                    "style":$("#style").val(),
                    "remark":$("#remark").val(),
                    "materialUnit":$("#materialUnit").val(),
                    "bagUnit":$("#bagUnit").val(),
                    "materialTypeId":$("#materialTypeId").val(),
                    "formula":$("#formula").val()
                },
                function (data) {
                    if(data == "success"){
                        $.messager.alert("操作提示", "保存成功！","info",function () {
                            window.location.href = "/material/list";
                        });
                    }else{
                        $.messager.alert("操作提示", "物料编码【"+$("#materialCode").val()+"】已存在，请重新输入！","error",function () {
                            $("#materialCode").focus();
                        });
                        return false;
                    }
                }
            );
        }

        //物料点击事件
        function click(value, res) {
            $("#materialId").val(res.materialId);
            $("#materialName").val(res.materialName);
            $("#materialCode").val(res.materialCode);
            $("#prodTypeId").val(res.prodTypeId);
            $("#materialTypeId").val(res.materialTypeId);
            $("#style").val(res.style);
            $("#remark").val(res.remark);
            $("#materialUnit").val(res.materialUnit);
            $("#formula").val(res.formula);
            $("#bagUnit").val(res.bagUnit);
        }

        //新增
        function reset() {
            $("#materialId").val("");
            $("#materialName").val("");
            $("#materialCode").val("");
            $("#style").val("");
            $("#remark").val("");
            $("#materialUnit").val("");
            $("#bagUnit").val("");
            $("#formula").val("");
        }

    </script>
</head>
<body>
<input type="hidden" id="current" th:value="${page.current}"/>
<input type="hidden" id="size" th:value="${page.size}"/>
<input type="hidden" id="total" th:value="${page.total}"/>
<table  class="easyui-datagrid" pagination="true" fitColumns="auto"  style="height:350px" singleSelect="true" data-options="onClickRow:click">
    <thead>
    <tr>
        <th field="materialTypeId" hidden>materialTypeId</th>
        <th field="prodTypeId" hidden>prodTypeId</th>
        <th align="center" width="60" field="materialId">序号</th>
        <th align="center" width="80" field="materialCode">物料编码</th>
        <th align="center" width="100" field="materialName">物料名称</th>
        <th align="center" width="80" field="materialTypeCode">物料类别编码</th>
        <th align="center" width="100" field="materialTypeName">物料类别名称</th>
        <th align="center" width="80" field="prodTypeCode">品种类别编码</th>
        <th align="center" width="100" field="prodTypeName">品种类别名称</th>
        <th align="center" width="100" field="style">规格</th>
        <th align="center" width="80" field="materialUnit">净重计量单位</th>
        <th align="center" width="80" field="bagUnit">包装计量单位</th>
        <th align="center" width="150" field="formula">批号规则</th>
        <th align="center" width="100" field="remark">物料描述</th>
        <th align="center" field="opt">操作</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="material:${page.data}">
        <td th:text="${material.materialTypeId}">
        </td>
        <td th:text="${material.prodTypeId}">
        </td>
        <td th:text="${material.materialId}"></td>
        <td th:text="${material.materialCode}"></td>
        <td th:text="${material.materialName}"></td>
        <td th:text="${material.materialTypeCode}"></td>
        <td th:text="${material.materialTypeName}"></td>
        <td th:text="${material.prodTypeCode}"></td>
        <td th:text="${material.prodTypeName}"></td>
        <td th:text="${material.style}"></td>
        <td th:text="${material.materialUnit}"></td>
        <td th:text="${material.bagUnit}"></td>
        <td th:text="${material.formula}"></td>
        <td th:text="${material.remark}"></td>
        <td>
            <a href="javascript:void(0);" class="easyui-linkbutton" plain="true" icon="icon-remove"  th:onclick="'del('+${material.materialId}+')'"> 删除</a>
        </td>
    </tr>
    </tbody>
</table>
<div id="tt" class="easyui-tabs" style="height:200px">
    <div title="物料管理" data-options="closable:false,cache:false" style="padding:0px;">
        <div class="newtitle">
            <th>
                <a class="easyui-linkbutton " href="javascript:void(0);" plain="true" icon="icon-add" onclick="reset();">新增</a>
                <a class="easyui-linkbutton " href="javascript:void(0);" plain="true" icon="icon-save" onclick="save();">保存</a>
            </th>
        </div>
        <form id="td" name="td" method="post" style="margin-left: 50px;">
            <hidden id="materialId" name="materialId"></hidden>
            <table border="0" width="100%">
                <tr height="40">
                    <td align="right"><label class="label-top">物料编码:</label></td>
                    <td><input class="easyui-textbox" data-options="required:true" id="materialCode" name="materialCode" style="width: 220px;">
                        <font color="red">必须唯一</font>
                    </td>
                    <td align="right"><label class="label-top">物料名称:</label></td>
                    <td><input class="easyui-textbox" data-options="required:true" id="materialName" name="materialName" style="width: 220px;"></td>
                    <td align="right"><label class="label-top">产品规格:</label></td>
                    <td><input class="easyui-textbox" data-options="required:true" id="style" name="style" style="width: 220px;"></td>
                </tr>
                <tr height="40">
                    <td align="right"><label class="label-top">净重计量单位:</label></td>
                    <td><input class="easyui-textbox" data-options="required:true" id="materialUnit" name="materialUnit" style="width: 220px;"></td>
                    <td align="right"><label class="label-top">所属品种:</label></td>
                    <td>
                        <select editable="false" id="prodTypeId" name="prodTypeId" style="width:220px;">
                            <option th:each="prodType:${prodTypeList}"
                                    th:value="${prodType.prodTypeId}"
                                    th:text="${prodType.prodTypeName}+'('+${prodType.prodTypeCode}+')'"
                            ></option>
                        </select>
                    </td>
                    <td align="right"><label class="label-top">所属物料类别:</label></td>
                    <td>
                        <select editable="false" id="materialTypeId" name="materialTypeId" style="width:220px;">
                            <option th:each="materialType:${materialTypeList}"
                                    th:value="${materialType.materialTypeId}"
                                    th:text="${materialType.materialTypeName}+'('+${materialType.materialTypeCode}+')'"
                            ></option>
                        </select>
                    </td>
                </tr>
                <tr height="40">
                    <td align="right"><label class="label-top">包装计量单位:</label></td>
                    <td><input class="easyui-textbox" data-options="required:true" id="bagUnit" name="bagUnit" style="width: 220px;"></td>
                    <td align="right"><label class="label-top">批号规则:</label></td>
                    <td ><input class="easyui-textbox" data-options="required:true" id="formula" name="formula" style="width: 220px;"></td>
                    <td align="right"><label class="label-top">物料描述:</label></td>
                    <td><input class="easyui-textbox" data-options="required:true" id="remark" name="remark" style="width: 220px;"></td>
                </tr>
            </table>
        </form>
    </div>
</div>
</body>
</html>